//
//  EditSortViewController.m
//  Remind
//
//  Created by macZF on 2017/10/28.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "EditSortViewController.h"
#import "EditSortTableViewCell.h"
#import "DataCach.h"
@interface EditSortViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *data;
}
@property (strong, nonatomic) UITableView *tableView;
@end

@implementation EditSortViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addSort)];
    //-------
    data = [NSMutableArray arrayWithArray:[DataCach GetTheSortedArray]];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(18, 18 + 64, CGRectGetWidth(self.view.frame) - 36, 50 * data.count) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.layer.cornerRadius = 15;
    self.tableView.layer.masksToBounds = YES;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EditSortTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"editCell"];
    [self.view addSubview:self.tableView];
    
}

- (void)addSort{
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"添加事件" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        [DataCach addTextFieldInputAccessoryView:textField];
        textField.placeholder = @"请输入分类名称";
        textField.borderStyle = UITextBorderStyleNone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [data addObject:alertCon.textFields[0].text];
        [DataCach setObject:data.lastObject forClassName:@"sort" IfTheDataIsNullInitialized:[NSArray class] ReplaceData:NO];
        [self redrawTableView];
        NSLog(@"%@",alertCon.textFields[0].text);
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    [alertCon addAction:okAction];
    [alertCon addAction:cancelAction];
    [self presentViewController:alertCon animated:YES completion:nil];
}

- (void)redrawTableView{
    if(_tableView){
    
        _tableView.frame = CGRectMake(18, 18 + 64, CGRectGetWidth(self.view.frame) - 36, 50 * data.count);
        [_tableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditSortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editCell"];
    cell.classNameLabel.text = data[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row > 3?YES:NO;
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull idx) {
        
        BOOL ok = [DataCach removeValueByClassName:data[indexPath.row]];
        if(ok){
            [data removeObjectAtIndex:indexPath.row];
            [DataCach setObject:data forClassName:@"sort" IfTheDataIsNullInitialized:[NSArray class] ReplaceData:YES];
            [DataCach getValueByClassName:@"sort"];
            [self redrawTableView];
        }
        //dispatch_sync(dispatch_get_main_queue(), ^{
        
        //});
    }];
    return @[action];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
